<?php

error_reporting(0);// Turn off all error reporting

class MySQLDB {

   private $con;
   private $host = '127.0.0.1';
   private $port = '3306';
   
   /** ---------LOCAL-------- **/
   private $user = 'root';
   private $pass = 'root';
   private $dbname = 'cementfoam';

   /** ---------PRODUCTION-------- **/
//     private $user = 'coolbloc_bfadmin';
//     private $pass = 'qSm04873Tbac';
//     private $dbname = 'cementfo_cementfoamDB';
    
    private $charset = 'utf8';

    function __construct() {
        $this->connect();
    }

    public function connect() {
        $this->con = mysqli_connect($this->host, $this->user, $this->pass);
        mysqli_set_charset($this->con, $this->charset);
        if (!$this->con) {
            die('Database Connection Failed!' . mysqli_connect_error());
        } else {
            mysqli_select_db($this->con, $this->dbname);
        }
    }

    public function query($sql) {
        return mysqli_query($this->con, $sql);
    }

    public function prepare($sql) {
        return mysqli_prepare($this->con, $sql);
    }

    public function execute_statement($statement) {
        return mysqli_stmt_execute($statement);
    }

    public function fetch_array($query) {
        if ($query === FALSE) {
            die(mysqli_error($this->con)); // TODO: better error handling
        }
        return mysqli_fetch_array($query);
    }

    public function fetch_statement($statement) {
        if ($statement === FALSE) {
            die(mysqli_error($this->con));
        }
        return mysqli_stmt_fetch($statement);
    }

    public function num_rows($query) {
        return mysqli_num_rows($query);
    }

    public function close_connection() {
        if (isset($this->con)) {
            mysqli_close($this->con);
            unset($this->con);
        }
    }

}

$db = new MySQLDB();
?>
